package com.qst.rongxiaoserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.rongxiaoserver.common.Result;
import com.qst.rongxiaoserver.model.FinanceModel;
import com.qst.rongxiaoserver.model.dto.FinanceDto;
import com.qst.rongxiaoserver.service.IFinanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/finance")
@Tag(name = "金融管理", description = "金融管理相关接口")
public class FinanceController {

    @Autowired
    IFinanceService financeService;

    // 个人贷款申请
    @PostMapping("/personal-apply")
    @Operation(summary = "个人贷款申请", description = "个人贷款申请")
    public Result<FinanceModel> personalApply(@Valid @RequestBody FinanceDto financeDto) {
        FinanceModel financeModel = new FinanceModel();
        BeanUtils.copyProperties(financeDto, financeModel);
        // 个人贷款不使用组合字段
        financeModel.setCombinationName1("");
        financeModel.setCombinationPhone1(null);
        financeModel.setCombinationIdnum1(null);
        financeModel.setCombinationName2(null);
        financeModel.setCombinationPhone2(null);
        financeModel.setCombinationIdnum2(null);

        boolean success = financeService.save(financeModel);
        return success ? Result.success(financeModel, "个人贷款申请成功") : Result.fail("申请失败");
    }

    // 组合贷款申请
    @PostMapping("/combination-apply")
    @Operation(summary = "组合贷款申请", description = "组合贷款申请")
    public Result<FinanceModel> combinationApply(@Valid @RequestBody FinanceDto financeDto) {
        FinanceModel financeModel = new FinanceModel();
        BeanUtils.copyProperties(financeDto, financeModel);

        boolean success = financeService.save(financeModel);
        return success ? Result.success(financeModel, "组合贷款申请成功") : Result.fail("申请失败");
    }

    // 修改金融信息
    @PostMapping("/update")
    @Operation(summary = "编辑金融信息", description = "编辑金融信息")
    public Result<FinanceModel> updateFinance(@Valid @RequestBody FinanceModel financeModel) {
//        // 设置更新时间
//        financeModel.setUpdateTime(new java.util.Date());
        boolean success = financeService.updateById(financeModel);
        return success ? Result.success(financeModel, "编辑成功") : Result.fail("编辑失败");
    }

    // 查询金融列表
    @GetMapping("/list")
    @Operation(summary = "金融列表", description = "金融列表")
    public Result<Page<FinanceModel>> getFinances(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                  @RequestParam(value = "realName", required = false) String realName) {
        Page<FinanceModel> financeList = financeService.getFinanceList(pageNum, pageSize, realName);
        return financeList != null ? Result.success(financeList, "查询成功") : Result.fail("查询失败");
    }

    // 删除金融记录
    @DeleteMapping("/delete")
    @Operation(summary = "根据ID删除金融记录", description = "根据ID删除金融记录")
    public Result deleteFinance(@Valid @RequestParam Integer financeId) {
        boolean success = financeService.removeById(financeId);
        return success ? Result.success("删除成功") : Result.fail("删除失败");
    }

    // 获取金融详情
    @GetMapping("/detail")
    @Operation(summary = "根据ID获取金融详情", description = "根据ID获取金融详情")
    public Result<FinanceModel> detailFinance(@Valid @RequestParam Integer financeId) {
        FinanceModel financeModel = financeService.getById(financeId);
        return financeModel != null ? Result.success(financeModel, "查询成功") : Result.fail("查询失败");
    }
}